package com.samebuilding.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.samebuilding.dao.PrivatePubMesDAO;
import com.samebuilding.domain.PrivatePublishMessage;
import com.samebuilding.domain.PrivateReceivedMessage;
import com.samebuilding.domain.User;

public class PrivatePubMesDAOImpl extends HibernateDaoSupport implements
PrivatePubMesDAO {

	
	private static final String FIND_ALL_PUB = "from PrivatePublishMessage as p where p.publisherUserno = ? order by p.date desc";
	private static final String FIND_ALL_PUB_SIZE = "select count(*) from PrivatePublishMessage as p where p.publisherUserno = ?";
	private static final String REMOVE_BY_ID = "delete from PrivatePublishMessage where id = ?";
	
	
	public void remove(PrivatePublishMessage ppm) {
		this.getHibernateTemplate().delete(ppm);
	}

	public void save(PrivatePublishMessage ppm) {
		this.getHibernateTemplate().save(ppm);
	}

	public List findAllPub(final User user ,final int from,final int size) {
		List message=this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query q=session.createQuery("from PrivatePublishMessage as p where p.publisherUserno=? order by p.date desc");
				q.setParameter(0, user.getUserNo());
				q.setFirstResult(from);
				q.setMaxResults(size);
				return q.list();
			}

		});
		return message;
	}

	public long findPubSize(User user) {
		List list=this.getHibernateTemplate().find("select count(*) from PrivatePublishMessage as p where p.publisherUserno=?",user.getUserNo());
		if(list!=null)
			return ((Long) list.iterator().next()).longValue();
		return 0;
		}

		public PrivatePublishMessage findById(String id) {
			List list=this.getHibernateTemplate().find("from PrivatePublishMessage as p where p.id=?",id);
			if(list!=null&&list.size()>0){
				return (PrivatePublishMessage)list.get(0);
			}
			return null;
		}

		@SuppressWarnings("unchecked")
		public List<PrivatePublishMessage> findAllPub(final int userNo, final int start,final int limit) {
			return (List<PrivatePublishMessage>)getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(FIND_ALL_PUB);
					query.setInteger(0, userNo);
					query.setFirstResult(start);
					query.setMaxResults(limit);
					List<PrivatePublishMessage> list = query.list();
					return list;
				}
			});
		}

		public PrivatePublishMessage findById(int id) {
			
			return (PrivatePublishMessage)this.getHibernateTemplate().get(PrivatePublishMessage.class, new Integer(id));
		}

		public int findPubSize(final int userNo) {
			long count = (Long)this.getHibernateTemplate().execute(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(FIND_ALL_PUB_SIZE);
					query.setInteger(0, userNo);

					return query.uniqueResult();
				}
			});
			return (int)count;
		}

		public void removeById(final int id) {
			getHibernateTemplate().execute(new HibernateCallback() {
				public Object doInHibernate(Session s)
						throws HibernateException, SQLException {
					Query query = s.createQuery(REMOVE_BY_ID);
					query.setInteger(0, id);
					query.executeUpdate();
					return null;
				}
			});
			
		}

	}
